package com.ruoyi.llm.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.llm.dao.ProjectInfoMapper;
import com.ruoyi.llm.domain.po.ProjectInfoPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
@DataSource(DataSourceType.MASTER)
public class ProjectInfoService  extends ServiceImpl<ProjectInfoMapper, ProjectInfoPo> {


    public ProjectInfoPo getProjectInfo(String docId){
        return this.getById(docId);
    }

    public List<ProjectInfoPo> listProjectInfo(String name){
        LambdaQueryWrapper<ProjectInfoPo> query=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name)){
            query.like(ProjectInfoPo::getName,name);
        }
        List<ProjectInfoPo> ret=this.list(query);
        return ret;
    }

    public String createProject(ProjectInfoPo projectInfo){
        if(projectInfo != null && StringUtils.isBlank(projectInfo.getId())){
            return "请填写应用标识";
        }
        if(projectInfo != null && StringUtils.isBlank(projectInfo.getName())){
            return "请填写应用名称";
        }
        if(projectInfo != null && StringUtils.isBlank(projectInfo.getUseModel())){
            return "请选择使用的大模型";
        }

        LambdaQueryWrapper<ProjectInfoPo>   query=new LambdaQueryWrapper<>();
        query.eq(ProjectInfoPo::getId,projectInfo.getId());
        List<ProjectInfoPo> proExistById = this.list(query);
        if(!proExistById.isEmpty()){
            return "应用标识号已经存在，请重新填写";
        }


        projectInfo.setCreator("管理员");
        // 创建一个Timestamp对象
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        projectInfo.setCreateTime(timestamp);
        projectInfo.setUpdateTime(timestamp);
        this.save(projectInfo);
        return "保存成功";

    }


    public String updateProject(ProjectInfoPo projectInfo){
        if(projectInfo != null && StringUtils.isBlank(projectInfo.getId())){
            return "请填写应用标识";
        }
        if(projectInfo != null && StringUtils.isBlank(projectInfo.getName())){
            return "请填写应用名称";
        }
        if(projectInfo != null && StringUtils.isBlank(projectInfo.getUseModel())){
            return "请选择使用的大模型";
        }

        // 创建一个Timestamp对象
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        projectInfo.setUpdateTime(timestamp);
        this.updateById(projectInfo);
        return "保存成功";

    }
}
